package autootje;

import java.awt.CardLayout;
import java.awt.SplashScreen;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

  // <editor-fold defaultstate="collapsed" desc="public static ...">
  public static JFrame frame;
  public static final int SYSTEM_LOOK = 0;
  public static final int NIMBUS_LOOK = 1;
  public static final int METAL_LOOK = 2;
  public static int huidigeLook = SYSTEM_LOOK;
  public static String propertiesFile = "frame.properties";
  public static int frameW = 640;
  public static int frameH = 480;
  public static boolean frameResizable = true;
  public static boolean dingFrameZichtbaar = false;
  public static boolean debug = false;
  public static boolean pda = false;
  public static final String progNaam = "Autootje";
  public static SplashScreen splash;
  public static boolean gebruikSplash = false; //nee, want het werkt niet
  //
  public static JPanel[] panels;
  public static final int aantalPanels = 10;
  public static final int START_SCHERM = 0;
  public static final int GAME_PANEL = 1;
  public static String vensterNaam = progNaam;
  // </editor-fold>

  public static void main(String[] args) {
    if (gebruikSplash) {
      maakSplashScreen();
    }
    checkPDA();
    commandline(args);
    deProperties();
    lookAndFeel(huidigeLook);
    maakFrame();
    maakPanels();
    showPane(START_SCHERM);
    //showPane(GAME_PANEL);
    frame.setVisible(true);
//    splash.close();
  }

  public static void maakSplashScreen() {
    //WERKT NIET......
    splash = SplashScreen.getSplashScreen();
    try {
      URL deUrl = Main.class.getResource("splash2.gif");
      splash.setImageURL(deUrl);
    } catch (Exception ex) {
      drukaf("SplashScreen FOUT !!!\n" + ex);
      ex.printStackTrace();
    }
  }

  public static void checkPDA() {
    if (System.getProperty("os.name").equals("Windows CE")) {
      pda = true;
    } else {
      pda = false;
    }
  }

  public static void maakPanels() {
    panels = new JPanel[aantalPanels];
    for (int a = 0; a < aantalPanels; a++) {
      panels[a] = new JPanel();
    }
    panels[START_SCHERM] = new StartScherm();
    panels[GAME_PANEL] = new GamePanel();
    for (int b = 0; b < aantalPanels; b++) {
      frame.add(panels[b], Integer.toString(b));
    }
  }

  public static void showPane(int welke) {
    if (debug) {
      drukaf("showPane... (" + welke + ")");
    }
    for (int a = 0; a < aantalPanels; a++) {
      panels[a].setVisible(false);
    }
    panels[welke].setVisible(true);
    panels[welke].requestFocus();

  }

  public static void drukaf(String t) {
    System.out.println(t);
  }

  public static void deProperties() {
    if (debug) {
      drukaf("properties...");
    }
    java.net.URL deURL = null;
    Properties props = new Properties();
    try {
      deURL = Main.class.getResource(propertiesFile);
      InputStream in = deURL.openStream();
      props.load(in);
      in.close();
    } catch (Exception ex) {
      System.out.println("Fout bij het lezen van de Properties-file\n(file: " + propertiesFile + ")\nFout: " + ex);
    }
    //Dingen doen
    if (debug) {
      drukaf("bezig met properties...");
    }
    //look and feel
    if (props.getProperty("lookAndFeel") != null && isInt(props.getProperty("lookAndFeel"))) {
      int foo = Integer.parseInt(props.getProperty("lookAndFeel"));
      if (foo < 0) {
        huidigeLook = 0;
      } else if (foo > 2) {
        huidigeLook = 2;
      } else {
        huidigeLook = foo;
      }
      if (Main.debug) {
        Main.drukaf("Main-Props: huidigeLook is nu: " + huidigeLook);
      }
    }
    //frameW
    if (props.getProperty("frameW") != null && isInt(props.getProperty("frameW"))) {
      int foo = Integer.parseInt(props.getProperty("frameW"));
      if (foo < 1) {
        frameW = 1;
      } else {
        frameW = foo;
      }
      if (Main.debug) {
        Main.drukaf("Main-Props: frameW is nu: " + frameW);
      }
    }
    //frameH
    if (props.getProperty("frameH") != null && isInt(props.getProperty("frameH"))) {
      int foo = Integer.parseInt(props.getProperty("frameH"));
      if (foo < 1) {
        frameH = 1;
      } else {
        frameH = foo;
      }
      if (Main.debug) {
        Main.drukaf("Main-Props: frameH is nu: " + frameH);
      }
    }
    //frameResizable
    if (props.getProperty("frameResizable") != null) {
      frameResizable = Boolean.parseBoolean(props.getProperty("frameResizable"));
    }
  }

  public static boolean isInt(String getal) {
    try {
      Integer.parseInt(getal);
    } catch (Exception e) {
      if (debug) {
        drukaf("isInt: " + getal + " is geen int getal !");
      }
      return false;
    }
    return true;
  }

  public static void lookAndFeel(int welke) {
    try {
      if (debug) {
        drukaf("lookAndFeel...");
      }
      if (welke == SYSTEM_LOOK) {
        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        //System
        if (debug) {
          drukaf("lookAndFeel: SYSTEM_LOOK");
        }
      } else if (welke == NIMBUS_LOOK) {
        javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        //Nimbus
        if (debug) {
          drukaf("lookAndFeel: NIMBUS_LOOK");
        }
      } else if (welke == METAL_LOOK) {
        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getCrossPlatformLookAndFeelClassName());
        //Metal
        if (debug) {
          drukaf("lookAndFeel: METAL_LOOK");
        }
      } else {
        drukaf("lookAndFeel: Fout: verkeerd argument (arg: " + welke + ")");
      }
    } catch (Exception ex) {
      drukaf("lookAndFeel: Fout: " + ex);
    }
  }

  public static void maakFrame() {
    frame = new JFrame();
    frame.setSize(frameW, frameH);
    frame.setResizable(frameResizable);
    frame.setTitle(vensterNaam);
    frame.setLayout(new CardLayout());
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent ev) {
        frameGesloten();
      }
    });
  }

  public static void frameGesloten() {
    //frame is gesloten
    if (debug) {
      drukaf("Venster is gesloten");
      drukaf("Programma wordt gestopt");
    }
    System.exit(0);
  }

  public static void commandline(String[] args) {

    drukaf(progNaam + "\nby FrankkieNL\n-h\t Commandline Opties\n--------------------");

    // <editor-fold defaultstate="collapsed" desc="Dit Was Met Getopt">
/*
    Getopt g = new Getopt("testprog", args, ":hd::D::pc");
    //
    int c;
    String arg;
    while ((c = g.getopt()) != -1) {
    switch (c) {

    case 'h': {
    printHelp();
    System.exit(0);
    break;
    }

    //
    case 'd': {
    arg = g.getOptarg();
    if (arg == null) {
    dingFrameZichtbaar = true;
    } else {
    dingFrameZichtbaar = Boolean.parseBoolean(arg);
    }
    break;
    }
    //

    case 'D': {
    arg = g.getOptarg();
    if (arg == null) {
    debug = true;
    } else {
    debug = Boolean.getBoolean(arg);
    }
    drukaf("debug = " + debug);
    break;
    }

    case 'p': {
    pda = true;
    if (debug) {
    drukaf("Force PDA-Mode");
    }
    break;
    }

    case 'c': {
    pda = false;
    if (debug) {
    drukaf("Force Computer-Mode");
    }
    break;
    }

    default:
    drukaf("getopt() returned " + c + "\n");
    break;
    }
    }
     */// </editor-fold>

    //Mijn versie is mischien onhandig
    //Maar werkt wel en is helemaal zelfgemaakt.
    String[] mogelijkeArgs = {"-help", "-debug", "-pda", "-comp"};

    if (args.length > 0) {
      //Er Zijn Args
      for (int a = 0; a < args.length; a++) {
        for (int b = 0; b < mogelijkeArgs.length; b++) {
          if (args[a].equalsIgnoreCase(mogelijkeArgs[b])) {
            doeCMDActie(b);
          }
        }
      }
    }
  }

  public static void doeCMDActie(int argNr) {
    //String[] mogelijkeArgs = {"-help","-debug","-pda","-comp"};
    switch (argNr) {
      case 0: {
        printHelp();
        System.exit(0);
        break;
      }
      case 1: {
        debug = true;
        break;
      }
      case 2: {
        pda = true;
        break;
      }
      case 3: {
        pda = false;
        break;
      }
    }
  }

  public static void printHelp() {
    drukaf("Commandline Opties:");
    drukaf("-help\tLaat Deze Help Zien");
    //drukaf("-d [true|false]\tDingFrame laten zien\n(Alleen als dit programma het dingFrame gebruikt)");
    drukaf("-debug\tDebug-mode");
    drukaf("-pda\tForce PDA-mode (als die er is)");
    drukaf("-comp\tForce Computer-mode (als die er is)");
  }
}
